package sort;

public class QuickSort {
    public static void main(String[] args) {
        int[] arr = {3,2,1,0,5,7,6,9,8,10};
        Sort(arr,0,arr.length - 1);
        for (int x: arr) {
            System.out.print(x + " ");
        }
    }
    public static void quickSort(int[] elem,int start,int end){
        if(start >= end){
            return;
        }
        //每次寻找基准值
        int reference = pointerReference(elem,start,end);
        //找左区间的基准值
        quickSort(elem,start,reference - 1);
        //找右区间的基准值
        quickSort(elem,reference+1,end);
    }
    //挖坑法
    public static int reference(int[] elem,int left,int right){
        int tmp = elem[left];
        while (left < right){
            while (left < right && tmp < elem[right]){
                right--;
            }
            elem[left] = elem[right];
            while (left < right && tmp > elem[right]){
                left ++;
            }
            elem[right] = elem[left];
        }
        elem[left] = tmp;
        return left;
    }
    public static int hoareReference(int[] elem,int left,int right){
        int tmp = elem[left];
        int i = left;
        while (left < right){
            while (left < right && tmp <= elem[right]){
                right--;
            }
            while (left < right && tmp >= elem[right]){
                left ++;
            }
            swap(left,right,elem);
        }
        swap(left,i,elem);
        return left;
    }
    public static void swap(int start,int end,int[] elem){
        int tmp = elem[start];
        elem[start] = elem[end];
        elem[end] = tmp;
    }
    public static int pointerReference(int[] elem,int left,int right){
        int prev = left;
        int cur = left + 1;
        while (cur <= right){
            if (elem[cur]  < elem[left] && elem[++prev] != elem[cur]){
                swap(cur,prev,elem);
            }
            cur++;
        }
        swap(prev,left,elem);
        return prev;
    }
    //用三位取中法对快排进行优化
    public static void Sort(int[] elem,int start,int end){
        int left = start;
        int right = end;
        if(left >= right){
            return;
        }
        //使用插入排序
        //20为随机取的值，具体情况根据使用场景来指定
        if(end - start < 2){
            insertSort(elem,left,right);
            return;
        }
        //找基准值的下标，使基准值左边的值小于基准值，基准值右边的值大于基准值
        //三数取中法找到更适合当基准值的数的下标
        int index = midThree(elem,start,end);
        //交换更合适当基准值的位置和排序开始的位置
        swap(start,index,elem);
        int tmp = elem[left];
        int benchmark = reference(elem,start,end);
        //通过递归再次在基准值左右两边再次找基准
        //找左边基准值
        quickSort(elem,start,benchmark-1);
        //找右边基准值
        quickSort(elem,benchmark+1,end);
    }
    //三数取中法
    public static int midThree(int[] array,int left,int right){
        int mid = (left+right)/2;
        if(array[left] < array[right]){
            if(array[mid] < array[left]){
                return left;
            }else if(array[mid] > array[right]){
                return right;
            }else {
                return mid;
            }
        }else{
            if(array[mid] > array[left]){
                return left;
            }else if(array[mid] < array[right]){
                return right;
            }else {
                return mid;
            }
        }
    }
    //插入排序
    public static void insertSort(int[] arr,int left,int right){
        for (int i = left + 1; i <= right; i++) {
            //记录待插入元素的下标
            int index = i;
            //待插入元素
            int cur = arr[i];
            //元素移动
            while (index > 0){
                //升序排序，后面的元素小于前面的就往前插入
                if(cur < arr[index - 1]){
                    arr[index] = arr[index - 1];
                    index--;
                }else {
                    break;
                }

            }
            //将待插入元素插入对应位置
            arr[index] = cur;
        }
    }
}
